//
//  JQFYMusicGenreViewController.m
//  JQFY
//
//  Created by 代辉 on 2017/10/30.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYMusicGenreViewController.h"
#import "JQFYGenreMusicInfoCollectionViewCell.h"

@interface JQFYMusicGenreViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,weak) UIImageView *topView;
@property (nonatomic,strong) NSMutableArray <UIButton *>*categoryButtons;
@property (nonatomic,weak) UIButton *currentButton;
@property (nonatomic,weak) UIScrollView *scrollView;
@property (nonatomic,weak) UICollectionView *genreView;
@end

static NSString *GENRE_CELL = @"GENRE_CELL";

@implementation JQFYMusicGenreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音乐流派";
    [self setupUI];
    [self setupScrollView];
    [self setupCollectionView];
}

- (NSMutableArray *)categoryButtons
{
    if (_categoryButtons == nil) {
        _categoryButtons = [NSMutableArray new];
    }
    return _categoryButtons;
}

- (void)setupUI
{
    UIImageView *topView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"genre_top"]];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
    }];
    
    self.topView = topView;
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"ELECTRONIC MUSIC";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont fontWithName:@"BebasNeue" size:30];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(74 *SCAL);
    }];
    UILabel *genreLabel = [UILabel new];
    genreLabel.text = @"GENRE";
    genreLabel.textColor = [UIColor whiteColor];
    genreLabel.font = [UIFont fontWithName:@"BebasNeue" size:30];
    [topView addSubview:genreLabel];
    [genreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(titleLabel.mas_bottom);
    }];
}

- (void)setupScrollView
{
    UIScrollView *scroll = [UIScrollView new];
    scroll.backgroundColor = [UIColor lightGrayColor];
    scroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scroll];
    self.scrollView = scroll;
    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    NSInteger width = DH_Screen_Width / 4;
    for (int i = 0; i < self.musicGenries.count; i ++ ) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * width,0 ,width, 40)];
        [button setTitle:[NSString stringWithFormat:@"%zd",i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHex:0x734EE9] forState:UIControlStateSelected];
        [scroll addSubview:button];
        button.tag = i;
        [self.categoryButtons addObject:button];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    scroll.contentSize = CGSizeMake(self.musicGenries.count *width, 40);
    self.currentButton = self.categoryButtons.firstObject;
    self.currentButton.selected = YES;
}

- (void)clickButton:(UIButton *)button
{
    if (self.currentButton == button) {
        return;
    }
    CGFloat offsetX = button.center.x - (self.view.bounds.size.width * 0.5);
    
    CGFloat minOffset = 0;
    CGFloat maxOffset = self.scrollView.contentSize.width - self.view.bounds.size.width;
    if (minOffset > offsetX) {
        
        offsetX = minOffset;
    }
    if (offsetX > maxOffset) {
        
        offsetX = maxOffset;
    }
    
    self.currentButton.selected = NO;
    button.selected = YES;
    [self.genreView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:button.tag inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    self.currentButton = button;
}

- (void)setupCollectionView
{
    [self.view layoutIfNeeded];
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.itemSize = CGSizeMake(DH_Screen_Width, DH_Screen_Height - CGRectGetMaxY(self.scrollView.frame));
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *genreView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [genreView registerClass:[JQFYGenreMusicInfoCollectionViewCell class] forCellWithReuseIdentifier:GENRE_CELL];
    genreView.delegate = self;
    genreView.dataSource = self;
    genreView.showsHorizontalScrollIndicator = NO;
    genreView.pagingEnabled = YES;
    [self.view addSubview:genreView];
    self.genreView = genreView;
    genreView.backgroundColor = [UIColor clearColor];
    [genreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / DH_Screen_Width;
    UIButton *selectButton = self.categoryButtons[index];
    
    if (self.currentButton.selected) {
        self.currentButton.selected = NO;
    }
    selectButton.selected = YES;
    CGFloat offsetX = selectButton.center.x - (self.view.bounds.size.width * 0.5);
    
    CGFloat minOffset = 0;
    CGFloat maxOffset = self.scrollView.contentSize.width - self.view.bounds.size.width;
    
    if (minOffset > offsetX) {
        
        offsetX = minOffset;
    }
    if (offsetX > maxOffset) {
        
        offsetX = maxOffset;
    }
    
    //让点击的button居中
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    self.currentButton = selectButton;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.musicGenries.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JQFYGenreMusicInfoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GENRE_CELL forIndexPath:indexPath];
    cell.songInfo = self.musicGenries;
    
    return cell;
}
@end
